package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.po.stu.CirclePublishPo;
import com.xjc.business.entity.vo.stu.CircleFriendsVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.StudentCircleFriendsMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【student_circle_friends(宿舍圈子)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class StudentCircleFriendsServiceImpl extends ServiceImpl<StudentCircleFriendsMapper, StudentCircleFriends>
    implements StudentCircleFriendsService {

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    AllStudentStayRelationsService allStudentStayRelationsService;

    @Autowired
    @Lazy
    StudentsService studentsService;

    @Autowired
    @Lazy
    StudentCircleFriendCommentsService studentCircleFriendCommentsService;

    @Autowired
    @Lazy
    CollegesService collegesService;

    @Autowired
    @Lazy
    MajorsService majorsService;

    @Autowired
    @Lazy
    ClazzsService clazzsService;

    /**
     * 发布朋友圈
     * @param circlePublishPo 信息
     * @return 反馈
     */
    @Override
    public Result<String> publishCircle(CirclePublishPo circlePublishPo) {
        Integer wxId = circlePublishPo.getWxId();
        Integer stuId = wxUserService.getById(wxId).getStuId();

        StudentCircleFriends studentCircleFriends = new StudentCircleFriends();
        studentCircleFriends.setPublishId(stuId);
        studentCircleFriends.setContent(circlePublishPo.getContent());
        studentCircleFriends.setPics(circlePublishPo.getPics());
        studentCircleFriends.setCanCommit(circlePublishPo.getCanCommit());
        studentCircleFriends.setIsAnonymity(circlePublishPo.getIsAnonymity());
        studentCircleFriends.setAnonymityName(circlePublishPo.getAnonymityName().trim().length()!=0? circlePublishPo.getAnonymityName() : "匿名");
        studentCircleFriends.setRanges(circlePublishPo.getRange());
        studentCircleFriends.setCreateTime(new Date());
        studentCircleFriends.setUpdateTime(new Date());
        save(studentCircleFriends);

        return Result.okWithData("发布成功");
    }

    /**
     * 获取朋友圈
     * @param wxId 微信用户id
     * @return 朋友圈列表
     */
    @Override
    public Result<List<CircleFriendsVo>> getCircleList(Integer wxId) {

        List<CircleFriendsVo> circleFriendsVoList = new ArrayList<>();

        Integer stuId = wxUserService.getById(wxId).getStuId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,stuId);
        AllStudentRegsiterRelations studentFromDb = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper);

        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,stuId);
        AllStudentStayRelations studentStayRelations = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);
        if(studentStayRelations == null){
            return Result.okWithData(circleFriendsVoList);
        }
        Integer buildId = studentStayRelations.getBuildId();



        Integer schoolId = studentFromDb.getSchId();
        Integer colId = studentFromDb.getColId();
        Integer majId = studentFromDb.getMajId();
        Integer clasId = studentFromDb.getClazzId();

        // 同学校

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper1.eq(AllStudentRegsiterRelations::getSchId,schoolId);
        List<Integer> sameSchoolStuIdList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper1).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());
        LambdaQueryWrapper<StudentCircleFriends> studentCircleFriendsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentCircleFriendsLambdaQueryWrapper.in(StudentCircleFriends::getPublishId,sameSchoolStuIdList);
        studentCircleFriendsLambdaQueryWrapper.eq(StudentCircleFriends::getRanges,3);
        List<StudentCircleFriends> studentCircleFriendsListSameSchool = list(studentCircleFriendsLambdaQueryWrapper);
        generateCircleList(circleFriendsVoList, colId, majId, clasId, studentCircleFriendsListSameSchool);

        // 同学院

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper2.eq(AllStudentRegsiterRelations::getColId,colId);
        List<Integer> sameColStuIdList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper2).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());
        LambdaQueryWrapper<StudentCircleFriends> studentCircleFriendsLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        studentCircleFriendsLambdaQueryWrapper1.in(StudentCircleFriends::getPublishId,sameColStuIdList);
        studentCircleFriendsLambdaQueryWrapper1.eq(StudentCircleFriends::getRanges,2);
        List<StudentCircleFriends> studentCircleFriendsListSameCol = list(studentCircleFriendsLambdaQueryWrapper1);
        generateCircleList(circleFriendsVoList, colId, majId, clasId, studentCircleFriendsListSameCol);


        // 同宿舍
        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper2.eq(AllStudentStayRelations::getBuildId,buildId);
        List<Integer> sameDormStuIdList = allStudentStayRelationsService.list(allStudentStayRelationsLambdaQueryWrapper2).stream().map(AllStudentStayRelations::getStuId).collect(Collectors.toList());
        LambdaQueryWrapper<StudentCircleFriends> studentCircleFriendsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        studentCircleFriendsLambdaQueryWrapper2.in(StudentCircleFriends::getPublishId,sameDormStuIdList);
        studentCircleFriendsLambdaQueryWrapper2.eq(StudentCircleFriends::getRanges,1);
        List<StudentCircleFriends> studentCircleFriendsListSameDorm = list(studentCircleFriendsLambdaQueryWrapper2);
        generateCircleList(circleFriendsVoList, colId, majId, clasId, studentCircleFriendsListSameDorm);

        circleFriendsVoList.sort((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime())); //按照发布时间排序

        return Result.okWithData(circleFriendsVoList);
    }

    private void generateCircleList(List<CircleFriendsVo> circleFriendsVoList, Integer colId, Integer majId, Integer clasId, List<StudentCircleFriends> studentCircleFriendsList) {
        for (StudentCircleFriends studentCircleFriends : studentCircleFriendsList) {
            CircleFriendsVo circleFriendsVo = new CircleFriendsVo();
            Integer publishId = studentCircleFriends.getPublishId();
            if(studentCircleFriends.getIsAnonymity() == 1){
                circleFriendsVo.setPublishName(studentsService.getById(publishId).getName());
            } else {
                circleFriendsVo.setPublishName(studentCircleFriends.getAnonymityName());
            }
            LambdaQueryWrapper<WxUser> wxUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
            wxUserLambdaQueryWrapper.eq(WxUser::getStuId,publishId);
            circleFriendsVo.setAvatar(wxUserService.getOne(wxUserLambdaQueryWrapper).getPic());
            if(studentCircleFriends.getIsAnonymity() == 1){
                String collegeName = collegesService.getById(colId).getName();
                String majName = majorsService.getById(majId).getName();
                String clazzName = clazzsService.getById(clasId).getNum() + "班";
                circleFriendsVo.setSiteInfo(collegeName+"学院 "+majName+"专业 "+clazzName);
            } else {
                circleFriendsVo.setSiteInfo("匿名");
            }

            circleFriendsVo.setContent(studentCircleFriends.getContent());
            circleFriendsVo.setPics(studentCircleFriends.getPics());
            circleFriendsVo.setCanCommit(studentCircleFriends.getCanCommit());
            circleFriendsVo.setIsAnonymity(studentCircleFriends.getIsAnonymity());
            if(studentCircleFriends.getCanCommit() == 1){
                LambdaQueryWrapper<StudentCircleFriendComments> studentCircleFriendCommentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                studentCircleFriendCommentsLambdaQueryWrapper.eq(StudentCircleFriendComments::getCircleId,studentCircleFriends.getId());
                studentCircleFriendCommentsLambdaQueryWrapper.orderByAsc(StudentCircleFriendComments::getCreateTime);
                List<CircleFriendsVo.CircleFriendComments> friendCommentsList = studentCircleFriendCommentsService.list(studentCircleFriendCommentsLambdaQueryWrapper).stream().map(studentCircleFriendComments -> {
                    CircleFriendsVo.CircleFriendComments circleFriendComments = new CircleFriendsVo.CircleFriendComments();
                    circleFriendComments.setCommentName(studentsService.getById(studentCircleFriendComments.getCommentId()).getName());
                    circleFriendComments.setContent(studentCircleFriendComments.getContent());
                    return circleFriendComments;
                }).collect(Collectors.toList());
                circleFriendsVo.setCircleFriendCommentsList(friendCommentsList);
            }
            circleFriendsVo.setCreateTime(studentCircleFriends.getCreateTime());

            circleFriendsVoList.add(circleFriendsVo);
        }
    }
}




